﻿using System;
using System . Collections . Generic;
using System . Collections . ObjectModel;
using System . IO;
using System . Linq;
using System . Reflection;
using System . Text;
using System . Threading . Tasks;
using System . Xml . Linq;
using WenceyWang . RichMan4L;
using WenceyWang . RichMan4L . Maps;
using WenceyWang . RichMan4L . Players;

namespace WenceyWang . RichMan4L . Players . Models
{
	public class PlayerModel : MapObject
	{

		public string Name { get; set; }

		public string Introduction { get; set; }

		public Uri Model { get; set; }

		public List<PlayerSaying> SayingWhenGained { get; private set; } = new List<PlayerSaying> ( );

		public PlayerSaying GetSayingWhenGained ( PlayerModel harmed )
		{
			List<PlayerSaying> temp = new List<PlayerSaying> ( SayingWhenGained . Where ( ( saying ) => saying . Player == harmed?.Name ) );

			if ( temp . Count != 0 )
			{
				return temp [ Game . Current . Randomer . Next ( 0 , temp . Count ) ];
			}
			else
			{
				return GetSayingWhenGained ( null );
			}
		}

		public List<PlayerSaying> SayingWhenHarmed { get; private set; } = new List<PlayerSaying> ( );

		public PlayerSaying GetSayingWhenHarmed ( PlayerModel gained )
		{
			List<PlayerSaying> temp = new List<PlayerSaying> ( SayingWhenHarmed . Where ( ( saying ) => saying . Player == gained?.Name ) );

			if ( temp . Count != 0 )
			{
				return temp [ Game . Current . Randomer . Next ( 0 , temp . Count ) ];
			}
			else
			{
				return GetSayingWhenHarmed ( null );
			}
		}


		public List<PlayerSaying> SayingWhenMeet { get; private set; } = new List<PlayerSaying> ( );

		public override MapSize Size { get { return MapSize . Small; } set { } }

		public PlayerSaying GetSayingWhenMeet ( PlayerModel player )
		{
			List<PlayerSaying> temp = new List<PlayerSaying> ( SayingWhenMeet . Where ( ( saying ) => saying . Player == player?.Name ) );

			if ( temp . Count != 0 )
			{
				return temp [ Game . Current . Randomer . Next ( 0 , temp . Count ) ];
			}
			else
			{
				return GetSayingWhenMeet ( null );
			}
		}

		public override void StartDay ( Calendars . GameDate nextDate )
		{

		}

		public override void UpdateView ( )
		{
			throw new NotImplementedException ( );
		}

		public override void EndToday ( )
		{
			throw new NotImplementedException ( );
		}

		public PlayerModel ( XElement saving ) : base ( saving )
		{

		}

		public PlayerModel ( string fileName )
		{

			if ( null == fileName )
			{
				throw new ArgumentNullException ( nameof ( fileName ) );
			}

			XDocument doc = ResourceHelper . LoadXmlDocument ( @"Player.Model.Resources." + fileName );


			var modelNode = doc . Root;

			Name = modelNode . Attribute ( nameof ( Name ) ) . Value;

			Introduction = modelNode . Attribute ( nameof ( Introduction ) ) . Value;

			Model = new Uri ( modelNode . Attribute ( "Model" ) . Value ?? "" );

			var tempSayingWhenGained = from p in modelNode . Element ( "SayingWhenGained" ) . Elements ( )
									   select new PlayerSaying ( p );
			SayingWhenGained . AddRange ( tempSayingWhenGained );

			var tempSayingWhenHarmed = from p in modelNode . Element ( "SayingWhenGained" ) . Elements ( )
									   select new PlayerSaying
									   ( p );

			SayingWhenHarmed . AddRange ( tempSayingWhenHarmed );

			var tempSayingWhenMeet = from p in modelNode . Element ( nameof ( SayingWhenMeet ) ) . Elements ( )
									 select new PlayerSaying ( p );
			SayingWhenMeet . AddRange ( tempSayingWhenMeet );

		}
	}
}
